var status = 0;
    function yf_file_star(opt, title, url, allow_num){
    	var path = '/static/js/yf_file';
        var cname = opt.attr('class');
        var html = '<div class="yf-container">';
            html += '<div class="yf-demo">';
            html += '<div class="yf-title">' + title + '</div>';
            html += '<ul id="ul_pics" class="ul_pics clearfix">';
          //  html += '<li class="yf-btn"><div class="img"><img src="' + path + '/upload-pic3.png" id="yf-btn-ot" title="选择图片"></div></li>';
            html += '<li class="yf-btn"><div class="img" title="选择图片" id="yf-btn-ot"><p class="yf-sign-p">+</p><p class="yf-add-tip">继续添加</p></div></li>';
            html += '</ul>';
            html += '<div class="yf-btn-group">';
            html += '<p class="yf-p-tip">一共0张图片，共计0KB</p>';
            html += '<a type="button" class="yf-btn-css yf-btn-y" data-type="upload">开始上传</a>';
            html += '<a type="button" class="yf-btn-css yf-btn-n">取消</a>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            $img = $('.up-img');
            $img.html(html).show().attr('data-type', cname);
            $('.yf-container').css({
            	"top" : ($img.height() - $('.yf-container').height()) / 2 + "px"
            });
        var uploader = new plupload.Uploader({//创建实例的构造方法
            runtimes: 'html5,flash,silverlight,html4', //上传插件初始化选用那种方式的优先级顺序
            browse_button: 'yf-btn-ot', // 上传按钮
            url: url, //远程上传地址
            flash_swf_url: path + '/plupload/Moxie.swf', //flash文件地址
            silverlight_xap_url: path + '/plupload/Moxie.xap', //silverlight文件地址
            filters: {
                max_file_size: '2mb', //最大上传文件大小（格式100b, 10kb, 10mb, 1gb）
                mime_types: [//允许文件上传类型
                    {title: "files", extensions: "jpg,png,gif,jpeg"}
                ]
            },
            multi_selection: true, //true:ctrl多文件上传, false 单文件上传
            init: {
                FilesAdded: function(up, files) { //文件上传前
                	
                	var temp_num = 9;
                	if(allow_num > 0)
                	{
                		temp_num = allow_num;
                	}
                	if ($("#ul_pics").children("li").length > temp_num || files.length > temp_num) {
                    	$.tipMessage("一次最多只能上传"+temp_num+"张图片", 2, 3000);
	                	 plupload.each(files, function(file) { //遍历文件
	                		 uploader.removeFile(file['id']);
	                     });
                       // uploader.destroy();
                    } else {
                        var li = '';
                        plupload.each(files, function(file) { //遍历文件
                            li += "<li id='" + file['id'] + "'><div class='yf-default-img'></div><div class='progress' style='display:none;'><span class='bar'></span><span class='percent'>0%</span></div><p class='yf-add-p' style='display:none;'>" + file['name'] + "</p><div class='yf-del' title='删除' data-id='"+file['id']+"' data-size='"+file['size']+"'>删除</div></li>";    
                        });
                        var snum = files.length;
                        var all_size = 0;
                        for(var i=0; i<snum; i++)
                        {
                        	all_size += parseInt(files[i].size);
                        }
                        all_size = accDiv(all_size, 1024).toFixed(2);
                        $(li).insertBefore('.yf-btn');
                        plupload.each(files, function(yf_file){
                        	previewImage(yf_file, function (imgsrc) {
                        		var pre_img = imgsrc;
                        		$('#'+yf_file['id']).find('.yf-default-img').css('background-image', "url('"+pre_img+"')");
                            })
                        });
                        $('.yf-btn-y').attr('data-type', 'upload').text('开始上传');
                        $('.yf-p-tip').text('一共'+snum+'张图片，共计'+all_size+'KB');
                    }
                },
                UploadProgress: function(up, file) { //上传中，显示进度条
                    var percent = file.percent;
                    $("#" + file.id).find('.bar').css({"width": percent + "%"});
                    $("#" + file.id).find(".percent").text(percent + "%");
                },
                FileUploaded: function(up, file, info) { //文件上传成功的时候触发
                    var temp = info.response;
                    var data = JSON.parse(temp);
                    if(data.error == 0)
                    {
                    	var img_path = data.content.path + "yf_" + data.content.savename;
                    	//var img_path = data.content.path + data.content.savename;
                        $("#" + file.id).html("<div class='img' style='background-image: url("+img_path+");background-position: center;background-size: cover;'><img style='display:none;' class='yf-img-val' src='" + img_path + "'/></div><div class='yf-del' title='删除' data-id='0' data-size='"+data.content.size+"'>删除</div>");
                        $('.yf-btn-y').attr('data-type', 'add').text('完成');
            /*            var del_num = 0;
                    	var del_size = 0;
                    	$('.ul_pics > li > .yf-del').each(function(){
                    		del_size += parseInt($(this).attr('data-size'));
                    		del_num++;
                    	});
                    	del_size = accDiv(del_size, 1024).toFixed(2);
                    	$('.yf-p-tip').text('一共'+del_num+'张图片，共计'+del_size+'KB');*/
                    }
                    else
                    {
                    	$.tipMessage(data.content, 2, 3000);
                    	$("#" + file.id).remove();
                    }
                    var del_num = 0;
                 	var del_size = 0;
                 	$('.ul_pics > li > .yf-del').each(function(){
                 		del_size += parseInt($(this).attr('data-size'));
                 		del_num++;
                 	});
                 	del_size = accDiv(del_size, 1024).toFixed(2);
                 	$('.yf-p-tip').text('一共'+del_num+'张图片，共计'+del_size+'KB');
                },
                Error: function(up, err) { //上传出错的时候触发
                	$.tipMessage(err.message, 1, 3000);
                }
            }
        });
        uploader.init();
        $img.on('click', '.yf-btn-y', function(){
            var _this = $(this);
            var type = _this.attr("data-type");
            if(type == 'upload')
            {
            	$('.progress').show();
                uploader.start();
            }
            else if(type == 'add')
            {
                var cn = $img.attr('data-type');
                var arr = '';
        /*        $('.yf-img-val').each(function(i){
                    var temp = $(this).attr('src');
                    arr += '<img src="' + temp + '">';
                });*/
                if(cn == 'yf_insert')
                {
                	$('.yf-img-val').each(function(i){
                        var temp = $(this).attr('src');
                        arr += '<input type="hidden" name="all_pic[]" value="' + temp + '">';
                        if(i == 0)
                        {
                        	$('.mainpic > img').attr('src', temp);
                        }
                    });
                	$('.mainpic').append(arr);
                }
                if(cn == 'yf_insert_copy')
                {
                	$('.yf-img-val').each(function(i){
                		var temp = $(this).attr('src');
                		if(i == 0)
                        {
                        	$('.mainpic > img').attr('src', temp);
                        	$('input[name=image]').val(temp);
                        }
                    });
                }
                if(cn == 'yf_update')
                {
                	$('.yf-img-val').each(function(i){
                        var temp = $(this).attr('src');
                        arr += '<li class="yf-img-move"><img src="' + temp + '"><div class="yf-move-window yf-move-del"><span>删除</span></div><input type="hidden" name="all_pic[]" value="' + temp + '"></li>';
                    });
                	$(arr).insertAfter('.yf-img-move:last');
                }
                if(cn == 'yf_addopt_btn')
                {
                	var count = 0;
                	$('.yf-img-value').each(function(v){
                		var val = $(this).val();
                		if(val == '')
                		{
                			count++;
                		}
                	});
                	var temp = [];
                	$('.yf-img-val').each(function(i){
                		var val = $(this).attr('src');
                        temp.push(val);
                	});
                	for(var z = 0; z < temp.length; z++)
                	{
                		if(z < count)
                		{
                			$('.yf-img-value').each(function(j){
                				var val = $(this).val();
                        		if(val == '')
                        		{
                        			$(this).val(temp[z]);
                        			$(this).parent().find('img').attr('src', temp[z]);
                        			return false;
                        		}
                			});
                		}
                		else
                		{
                			$('.yf_add_ot').click();
                			$('.yf-img-value').each(function(j){
                				var val = $(this).val();
                        		if(val == '')
                        		{
                        			$(this).val(temp[z]);
                        			$(this).parent().find('img').attr('src', temp[z]);
                        			return false;
                        		}
                			});
                		}
                	}
                }
                $img.html("").hide();
            }
        });
        $img.on({ 
		    mouseenter: function() { 
		    	$(this).find('.yf-del').show();
		    }, 
		    mouseleave: function() { 
		    	$(this).find('.yf-del').hide();
		    }
		}, '.ul_pics > li');
        if(status == 0)
        {
            $img.on('click', '.yf-del', function(){
            	var _this = $(this);
            	var size = _this.attr('data-size');
            	var del_num = 0;
            	var del_size = 0;
            	
            	var fileid = _this.attr('data-id');
            	if(fileid != '')
            	{
            		var toremove = '';
                    var id = fileid;
                    for(var i=0; i<uploader.files.length; i++){
                        if(uploader.files[i].id === id){
                            toremove = i;
                        }
                    }
                    uploader.files.splice(toremove, 1);    
                    uploader.refresh();
            	}
            	
            	$('.ul_pics > li > .yf-del').each(function(){
            		del_size += parseInt($(this).attr('data-size'));
            		del_num++;
            	});
            	del_num = del_num - 1;
            	del_size = del_size - parseInt(size);
            	del_size = accDiv(del_size, (1024*1024)).toFixed(2);
            	$('.yf-p-tip').text('一共'+del_num+'张图片，共计'+del_size+'M');
                _this.parent().remove();
                var del_count = 0;
                var yf_id = 0;
                $('.ul_pics > li > .yf-del').each(function(){
                	yf_id = $(this).attr('data-id');
                	if(yf_id != '0')
                	{
                		del_count++;
                	}
                });
                if(del_count == 0)
                {
                	 $('.yf-btn-y').attr('data-type', 'add').text('完成');
                }
                else
                {
                	$('.yf-btn-y').attr('data-type', 'upload').text('开始上传');
                }
            });

            $img.on('click', '.yf-btn-n', function(){
                if(confirm("如果有上传图片则不会保存信息,确定取消么？"))
                {
                    $img.html("").hide();
                }
            });
            status++;
        }
        function previewImage(file, callback) {//file为plupload事件监听函数参数中的file对象,callback为预览图片准备完成的回调函数
            if (!file || !/image\//.test(file.type)) return; //确保文件是图片
            if (file.type == 'image/gif') {//gif使用FileReader进行预览,因为mOxie.Image只支持jpg和png
                var fr = new mOxie.FileReader();
                fr.onload = function () {
                    callback(fr.result);
                    fr.destroy();
                    fr = null;
                }
                fr.readAsDataURL(file.getSource());
            } else {
                var yf_preloader = new mOxie.Image();
                yf_preloader.onload = function () {
                    var imgsrc = yf_preloader.type == 'image/jpeg' ? yf_preloader.getAsDataURL('image/jpeg', 80) : yf_preloader.getAsDataURL(); //得到图片src,实质为一个base64编码的数据
                    callback && callback(imgsrc); //callback传入的参数为预览图片的url
                    yf_preloader.destroy();
                    yf_preloader = null;
                };
                yf_preloader.load(file.getSource());
            }
        }  
    }
 
function accDiv(arg1,arg2){ 
	var t1=0,t2=0,r1,r2; 
	try{t1=arg1.toString().split(".")[1].length}catch(e){} 
	try{t2=arg2.toString().split(".")[1].length}catch(e){} 
	with(Math){ 
	r1=Number(arg1.toString().replace(".","")) 
	r2=Number(arg2.toString().replace(".","")) 
	return (r1/r2)*pow(10,t2-t1); 
	}
} 